package search

import (
	"github.com/gin-gonic/gin"
	"math/rand"
	"moments/global"
	"moments/handle/request"
	"moments/pkg"
	"net/http"
	"strconv"
	"time"
)

func SendSms(c *gin.Context) {
	var form request.Sms
	if err := c.ShouldBind(&form); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	sms := rand.Intn(9000) + 1000
	upload, err := pkg.Upload(form.Phone, strconv.Itoa(sms))
	if err != nil {
		return
	}

	if upload.Code != 2 {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "短信发送失败",
		})
		return
	}

	global.Rdb.Set(global.Ctx, "sendSms_"+form.Phone, sms, time.Minute*30)

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "短信发送成功",
	})

}
